package android.group.one;

import java.io.IOException;
import java.util.Vector;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class math extends Activity  {
	
	EditText timer;
	Chronometer c;	
	
	RadioGroup option_group;
	RadioButton option;
	
	Button next;
	Button tomain;
	EditText question;
	//EditText answer;
	LinearLayout answer;
	
	PopupWindow popup;
	EditText popup_message;
	Button popup_close;
	LinearLayout popup_layout;

	Vector<String> question_list;
	Vector<String> answer_list;
	Vector<Vector> option_list;
	Vector<String> user_answer = new Vector<String>();
	Vector<RadioButton> optionVector;

	String answers = "";
	int index = 0;
	int index_option = 0;
	
	String level = "";
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.math);
        
        init();
        
        next = (Button) findViewById(R.id.math_next);
        tomain = (Button) findViewById(R.id.math_previous);
        question = (EditText) findViewById(R.id.math_question);
        answer = (LinearLayout) findViewById(R.id.math_layout);
        
        timer = new EditText(this);
		timer.setBackgroundColor(Color.GRAY);
        c = (Chronometer) findViewById(R.id.math_timer);
        c.start();

        next.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Perform action on click
            	if(check_selection()){
            		for(int i=0; i<option_group.getChildCount(); i++){
            			RadioButton o= (RadioButton)option_group.getChildAt(i);
            			if(o.isChecked()){
            				user_answer.add(o.getText().toString());
            				Log.w("myApp",o.getText().toString());
            			}
            		}

            		if(index < question_list.size()){
            			nextQuestion();
            		} else {
            			float rate = 0;
            			float total = answer_list.size();
            			float right = 0;
            			/* testing */
            			for(int i=0; i<answer_list.size(); i++){
            				Log.w("myApp", "answer list:"+answer_list.elementAt(i));
            				Log.w("myApp", "answer:"+user_answer.elementAt(i));
            			}
            			/* testing */
            			if(answer_list.size() == user_answer.size()){
            				for(int i=0; i<answer_list.size(); i++){
            					if(answer_list.elementAt(i).trim().equals(user_answer.elementAt(i).trim())){
            						right++;
            					}
            				}
            				rate = (right/total)*100;
            			}
        			
            			Intent myIntent = new Intent(v.getContext(), result.class);
            			myIntent.putExtra("result", Float.toString(rate));
            			myIntent.putExtra("time", c.getText().toString());
            			myIntent.putExtra("correct_answer", Float.toString(right) + "/" + Float.toString(total));
            			startActivity(myIntent);
            		}
            	} else {
            		//popup.showAtLocation(popup_layout, Gravity.BOTTOM, 10, 10);
            		//popup.setContentView(popup_layout);
            	}
            }
        });	
        
        tomain.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Perform action on click
            	Intent myIntent = new Intent(v.getContext(), Android_group_proj1Activity.class);
            	startActivity(myIntent);
            }
        });	
		
		try { 
		    SAXParserFactory spf = SAXParserFactory.newInstance(); 
		    SAXParser sp = spf.newSAXParser(); 
		 
		    XMLReader xr = sp.getXMLReader(); 
		 
		    DataHandler dataHandler = new DataHandler("question_math"); 
		    xr.setContentHandler(dataHandler); 
		 
		    xr.parse(new InputSource(this.getAssets().open("question.xml"))); 
		    
		    question_list = dataHandler.getQuestionList();
		    option_list = dataHandler.getOptionList();
		    answer_list = dataHandler.getAnswerList();		    
		    answer.setBackgroundColor(Color.RED);
		    nextQuestion();
		    
		  } catch(ParserConfigurationException pce) { 
		    Log.e("SAX XML", "sax parse error", pce); 
		  } catch(SAXException se) { 
		    Log.e("SAX XML", "sax error", se); 
		  } catch(IOException ioe) { 
		    Log.e("SAX XML", "sax parse io error", ioe); 
		}
	}
	
	public void init(){
		Bundle extra = getIntent().getExtras();
		if(extra != null){
			level = extra.getString("result");
		}
		popup = new PopupWindow(this);
		popup_message = new EditText(this);
		popup_message.setText("test");
		popup_close = new Button(this);
		popup_close.setText("close");
		popup_layout = new LinearLayout(this);
		popup_layout.addView(popup_message);
		popup_layout.addView(popup_close);
	}
	
	public boolean check_selection(){
		for(int i=0; i<option_group.getChildCount(); i++){
			RadioButton o= (RadioButton)option_group.getChildAt(i);
			if(o.isChecked()){
				return true;
			}
		}
		return false;
	}
	
	public void nextQuestion(){
    	question.setText(question_list.elementAt(index)); 
    	answer.removeAllViews();
        option_group = new RadioGroup(this);	    
        for(int i=0; i<option_list.elementAt(index).size(); i++){
        	String temp = "";
        	temp += option_list.elementAt(index).elementAt(i);
        	option = new RadioButton(this);
        	option.setText(temp);

        	option_group.addView(option);
        	/*
        	option.setOnClickListener(new View.OnClickListener() {
	            public void onClick(View v) {
	                // Perform action on click
	            	answers = option.getText().toString();
	            	EditText t = (EditText) findViewById(R.id.testing_answer);
	            	EditText vv = (EditText) findViewById(R.id.testing);
	            	t.setText(answers);
	            	vv.setText(answer_list.elementAt(0));
	            }
	        });	
	        */
        }
        answer.addView(option_group);
    	index += 1;
	}
}